package com.tjhk.demo.listener;

import com.rabbitmq.client.Channel;
import com.tjhk.demo.contants.RabbitMqExchangeConstants;
import com.tjhk.demo.contants.RabbitMqQueueConstants;
import com.tjhk.demo.contants.RabbitMqRoutingKeyConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitMqListener {
    public final static Logger log = LoggerFactory.getLogger(RabbitListener.class);
    /**
     *  声明队列
     *  动态绑定路由和路由KEY
     *  FANOUT 不需要路由KEY 广播模式
     *  queue名称不同时接收相同的数据，多个listener使用相同的queue名称则接收不同的数据
     *  若想接收全部数据则使用不同的queue名称相当于广播模式接收所有数据
     *  组内数据不从可多个服务使用相同queue名称则组内接收所有数据，实例见topic listener
     */

    // 测试版不必监听
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqQueueConstants.FANOUT_EXCHANGE_QUEUE_NAME,autoDelete = "false"),
            exchange = @Exchange(value = RabbitMqExchangeConstants.DEFAULT_FANOUT_EXCHANGE_NAME,type = ExchangeTypes.FANOUT)
    ))
    public void simple(Message message,Channel channel){
        try {
            byte[] body = message.getBody();
            System.out.println(new String(body));
//            //手动确认消息已经被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("RabbitMq---->CYC消费消息：" + message.toString() + "。成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("RabbitMq---->CYC消费消息：" + message.toString() + "。失败！");
        }
    }

    // 测试版不必监听
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqQueueConstants.FANOUT_EXCHANGE_QUEUE_NAME,autoDelete = "false"),
            exchange = @Exchange(value = RabbitMqExchangeConstants.DEFAULT_FANOUT_EXCHANGE_NAME,type = ExchangeTypes.FANOUT),
            key = "inform.1.fanout.1"
    ))
    public void simple2(Message message,Channel channel){
        try {
            byte[] body = message.getBody();
            System.out.println(new String(body));
//            //手动确认消息已经被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("RabbitMq2---->CYC消费消息：" + message.toString() + "。成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("RabbitMq2---->CYC消费消息：" + message.toString() + "。失败！");
        }
    }

    // 测试版不必监听
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqQueueConstants.DIRECT_EXCHANGE_QUEUE_NAME,autoDelete = "false"),
            exchange = @Exchange(value = RabbitMqExchangeConstants.DEFAULT_DIRECT_EXCHANGE_NAME),
            key = RabbitMqRoutingKeyConstants.DIRECT_ROUTING_KEY
    ))
    public void direct1(Message message,Channel channel){
        try {
            byte[] body = message.getBody();
            System.out.println(new String(body));
//            //手动确认消息已经被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("RabbitMq--direct-->CYC消费消息：" + message.toString() + "。成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("RabbitMq---->CYC消费消息：" + message.toString() + "。失败！");
        }
    }

    // 测试版不必监听
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqQueueConstants.DIRECT_EXCHANGE_QUEUE_NAME+1,autoDelete = "false"),
            exchange = @Exchange(value = RabbitMqExchangeConstants.DEFAULT_DIRECT_EXCHANGE_NAME),
            key = RabbitMqRoutingKeyConstants.DIRECT_ROUTING_KEY
    ))
    public void direct2(Message message,Channel channel){
        try {
            byte[] body = message.getBody();
            System.out.println(new String(body));
//            //手动确认消息已经被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("RabbitMq2--direct-->CYC消费消息：" + message.toString() + "。成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("RabbitMq2---->CYC消费消息：" + message.toString() + "。失败！");
        }
    }

    // 测试版不必监听
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqQueueConstants.TOPIC_EXCHANGE_QUEUE_NAME,autoDelete = "false"),
            exchange = @Exchange(value = RabbitMqExchangeConstants.TOPIC_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = "info.cyc.*"
    ))
    public void topic1(Message message,Channel channel){
        try {
            byte[] body = message.getBody();
            System.out.println(new String(body));
//            //手动确认消息已经被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("RabbitMq1-1---->CYC消费消息：" + message.toString() + "。成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("RabbitMq---->CYC消费消息：" + message.toString() + "。失败！");
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqQueueConstants.TOPIC_EXCHANGE_QUEUE_NAME,autoDelete = "false"),
            exchange = @Exchange(value = RabbitMqExchangeConstants.TOPIC_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = "info.cyc.*"
    ))
    public void topic2(Message message,Channel channel){
        try {
            byte[] body = message.getBody();
            System.out.println(new String(body));
//            //手动确认消息已经被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("RabbitMq1-2---->CYC消费消息：" + message.toString() + "。成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("RabbitMq2---->CYC消费消息：" + message.toString() + "。失败！");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqQueueConstants.TOPIC_EXCHANGE_QUEUE_NAME+1,autoDelete = "false"),
            exchange = @Exchange(value = RabbitMqExchangeConstants.TOPIC_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = "info.cyc.info"
    ))
    public void topic3(Message message,Channel channel){
        try {
            byte[] body = message.getBody();
            System.out.println(new String(body));
//            //手动确认消息已经被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("RabbitMq2-1--info->CYC消费消息：" + message.toString() + "。成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("RabbitMq2---->CYC消费消息：" + message.toString() + "。失败！");
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqQueueConstants.TOPIC_EXCHANGE_QUEUE_NAME+1,autoDelete = "false"),
            exchange = @Exchange(value = RabbitMqExchangeConstants.TOPIC_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = "info.cyc.info"
    ))
    public void topic4(Message message,Channel channel){
        try {
            byte[] body = message.getBody();
            System.out.println(new String(body));
//            //手动确认消息已经被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("RabbitMq2-2---info->CYC消费消息：" + message.toString() + "。成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("RabbitMq2-2--->CYC消费消息：" + message.toString() + "。失败！");
        }
    }
}
